
import datetime
import threading
import time
import logging

from datastore.resource import *
from datastore.uri import *
from runtime.task import *

class SupportTask(Task):
    """
    Task provide developer support.
    """
    
    def cb_start(self, method, uri, res, result, spaces_context, **kwargs):
        # test access to options.
        task_def = spaces_context.get(res)
        self.logger.log(logging.INFO, "Test option (%s)" % \
                        task_def.options.test)
        
        # wait until every settles down.
        time.sleep(3)
        
        # print stuff from spaces.
        #for uri in spaces_context.find_all():
        #    self.logger.log(logging.INFO, "Spaces URI %s" % (uri))
        #self.logger.log(logging.INFO, "-" * 40)
        
        # time_task tests
        rs = spaces_context.get(Uri('internal:base/services/time_task/timestamp'))
        if rs is None:
            self.logger.log(logging.INFO, "time_task:timestamp not available.")
        else:
            self.logger.log(logging.INFO, "Timestamp1 = %s" % rs.timestamp)
        rs = spaces_context.get(Uri('internal:base/services/time_task/timestamp'))
        if rs is None:
            self.logger.log(logging.INFO, "time_task:timestamp not available.")
        else:
            self.logger.log(logging.INFO, "Timestamp2 = %s" % rs.timestamp)
        self.logger.log(logging.INFO, "-" * 40)

        # list threads
        for thread in threading.enumerate():
            self.logger.log(logging.INFO, "Thread %s (daemon=%s)" % (thread.name, thread.daemon))
        self.logger.log(logging.INFO, "-" * 40)
            
        # shutdown cleanly
        time.sleep(1)
        spaces_context.put(Uri('internal:base/runtime/node/shutdown/quit'),
                           Resource(type='term:base/runtime/node_control'))
